home *** CD-ROM | disk | FTP | other *** search
- unit ClientMainFormUnit;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls;
-
- type
- TForm1 = class(TForm)
- Memo1: TMemo;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure Memo1Change(Sender: TObject);
- private
- Mailslot: THandle;
- end;
-
- {$ifdef Ver90}
- //This exception class did not exist in Delphi 2
- EWin32Error = class(Exception);
- {$endif}
-
- var
- Form1: TForm1;
-
- const
- MailslotNameLocalWritePrefix = '\\.\mailslot\';
- MailslotName = 'SampleMailslot';
- MailslotWriteName = MailslotNameLocalWritePrefix + MailslotName;
-
- implementation
-
- {$R *.DFM}
-
- function Win32Check(RetVal: Bool): Bool;
- var
- LastError: DWORD;
- begin
- Result := RetVal;
- if not RetVal then
- begin
- LastError := GetLastError;
- if LastError <> ERROR_SUCCESS then
- raise EWin32Error.CreateFmt( 'Win32 Error. Code: %d.'#10'%s',
- [LastError, SysErrorMessage(LastError)])
- else
- raise EWin32Error.Create('A Win32 API function failed')
- end;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Mailslot := CreateFile(
- MailslotWriteName, Generic_Write, File_Share_Read,
- nil, Open_Existing, File_Attribute_Normal, 0);
- if Mailslot = Invalid_Handle_Value then
- raise EWin32Error.Create('Cannot open client side of mailslot');
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- CloseHandle(Mailslot);
- end;
-
- procedure TForm1.Memo1Change(Sender: TObject);
- var
- BytesWritten: DWord;
- Msg: String;
- begin
- Msg := Memo1.Text;
- Win32Check(WriteFile(Mailslot, Msg[1], Length(Msg),
- BytesWritten, nil));
- end;
-
- end.